package com.nand.library.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nand.library.base.adapter.holder.BaseViewHolder;

import java.util.ArrayList;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by 孝杰 on 2019/12/17.
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    protected Context mContext;//上下文
    protected ArrayList<T> mData;//数据集合
    private int mDefaultResId;//默认layout 布局
    private int mSecondResId;//第二个layout 布局
    protected OnBaseItemClickListener mOnBaseItemClickListener;//item点击事件监听
    private OnBaseItemLongClickListener mOnBaseItemLongClickListener;//item长按事件监听
    public static final int TYPE_SECOND = Integer.MIN_VALUE;//多布局Type标志

    public interface OnBaseItemClickListener {
        void onBaseItemClick(View view, int position);
    }

    public interface OnBaseItemLongClickListener {
        void onBaseItemLongClick(View view, int position);
    }

    public BaseAdapter(Context mContext, ArrayList<T> mData, @LayoutRes int resId) {
        this.mContext = mContext;
        this.mData = mData;
        this.mDefaultResId = resId;
    }

    public BaseAdapter(Context mContext, ArrayList<T> mData, @LayoutRes int resId, @LayoutRes int secondResId) {
        this.mContext = mContext;
        this.mData = mData;
        this.mDefaultResId = resId;
        this.mSecondResId = secondResId;
    }

    public void setOnBaseItemClickListener(OnBaseItemClickListener onBaseItemClickListener) {
        this.mOnBaseItemClickListener = onBaseItemClickListener;
    }

    public void setOnBaseItemLongClickListener(OnBaseItemLongClickListener onBaseItemLongClickListener) {
        this.mOnBaseItemLongClickListener = onBaseItemLongClickListener;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_SECOND) {
            return new BaseViewHolder(LayoutInflater.from(mContext).inflate(mSecondResId, parent, false), viewType);
        }
        return new BaseViewHolder(LayoutInflater.from(mContext).inflate(mDefaultResId, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        bindHolder(mContext, holder, mData, position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnBaseItemClickListener != null) {
                    mOnBaseItemClickListener.onBaseItemClick(holder.itemView, holder.getAdapterPosition());
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnBaseItemLongClickListener != null) {
                    mOnBaseItemLongClickListener.onBaseItemLongClick(holder.itemView, holder.getAdapterPosition());
                    return true;
                }
                return false;
            }
        });
    }

    protected abstract void bindHolder(Context mContext, BaseViewHolder holder, ArrayList<T> mData, int position);

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 设置数据
     */
    public void setData(ArrayList<T> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    /**
     * 获取数据
     */
    public ArrayList<T> getData() {
        return mData;
    }
}
